/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "imgops/thresholding/CvAdaptiveThresholdOp.h"
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;

namespace NAMESPACE{

// ****** Functor implementation ***********************************************
bool CvAdaptiveThresholdFunc::operator()(const cv::Mat& src,
                                         cv::Mat& dst,
                                         double maxValue,
                                         int adaptiveMethod,
                                         int thresholdType,
                                         int blockSize,
                                         double C){
    Mat m = src;
    if(src.type() == cv::DataType<double>::type){
        double max = *std::max_element(src.begin<double>(), src.end<double>());
        src.convertTo(m, cv::DataType<uchar>::type, 255.0 / max);
    }
    adaptiveThreshold(m, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C);
    return true;
}
// -----------------------------------------------------------------------------

// ****** Constants assignment *************************************************
const size_t CvAdaptiveThresholdOp::MIN_INPUT_NUM = 1;
const size_t CvAdaptiveThresholdOp::MIN_CRITERIA_NUM = 5;
const size_t CvAdaptiveThresholdOp::MIN_OUTPUT_NUM = 1;
// -----------------------------------------------------------------------------

// ****** Operation implementation *********************************************
CvAdaptiveThresholdOp::~CvAdaptiveThresholdOp(){}
CvAdaptiveThresholdOp::CvAdaptiveThresholdOp() : AbstractOperation<CvAdaptiveThresholdFunc>(){

    min_input_num = MIN_INPUT_NUM;
    min_criteria_num = MIN_CRITERIA_NUM;
    min_output_num = MIN_OUTPUT_NUM;

    k1.set_key(INPUT_NS,  "img"); k1.setType(TypeParseTraits<cv::Mat*>::name());
    k2.set_key(OUTPUT_NS, "img"); k2.setType(TypeParseTraits<cv::Mat*>::name());

    k3.set_key(CRITERIA_NS, "value.max");   k3.setType(TypeParseTraits<double>::name());
    k4.set_key(CRITERIA_NS, "constant.1");  k4.setType(TypeParseTraits<int>::name());
    k5.set_key(CRITERIA_NS, "constant.2");  k5.setType(TypeParseTraits<int>::name());
    k6.set_key(CRITERIA_NS, "size.width");  k6.setType(TypeParseTraits<int>::name());
    k7.set_key(CRITERIA_NS, "value.shift"); k7.setType(TypeParseTraits<double>::name());

    input_keys->push_back(k1);
    output_keys->push_back(k2);

    criteria_keys->push_back(k3);
    criteria_keys->push_back(k4);
    criteria_keys->push_back(k5);
    criteria_keys->push_back(k6);
    criteria_keys->push_back(k7);
}
CvAdaptiveThresholdOp::CvAdaptiveThresholdOp(const CvAdaptiveThresholdOp& gbo) : AbstractOperation<CvAdaptiveThresholdFunc>(gbo){}
CvAdaptiveThresholdOp& CvAdaptiveThresholdOp::operator=(const CvAdaptiveThresholdOp& gbo){
    if(this == &gbo) return *this;
    AbstractOperation<CvAdaptiveThresholdFunc>::operator=(gbo);
    return *this;
}

bool CvAdaptiveThresholdOp::doOperation(const std::list<input_t>& inputs,
                              const std::list<criteria_t>& operationCriteria,
                              std::list<output_t>& outputs) throw(bad_io_mapping){

    AbstractOperation<CvAdaptiveThresholdFunc>::prepareIO(inputs, operationCriteria, outputs);

    cv::Mat* in_img  = boost::any_cast<cv::Mat*>( ((*input_mapping) [k1])->getElement() );
    cv::Mat* out_img = boost::any_cast<cv::Mat*>( ((*output_mapping)[k2])->getElement() );

    double max_value    = boost::any_cast<double>( ((*criteria_mapping)[k3])->getElement() );
    int adaptive_method = boost::any_cast<int>(    ((*criteria_mapping)[k4])->getElement() );
    int threshold_type  = boost::any_cast<int>(    ((*criteria_mapping)[k5])->getElement() );
    int block_size      = boost::any_cast<int>(    ((*criteria_mapping)[k6])->getElement() );
    double C            = boost::any_cast<double>( ((*criteria_mapping)[k7])->getElement() );

    (*operation)(*in_img, *out_img, max_value, adaptive_method, threshold_type, block_size, C);

    return true;
}

bool CvAdaptiveThresholdOp::operator==(const AbstractOperation<operation_t>& op) const{
    return AbstractOperation<CvAdaptiveThresholdFunc>::operator==(op);
}

bool CvAdaptiveThresholdOp::operator!=(const AbstractOperation<operation_t>& op) const{
    return !operator==(op);
}
// -----------------------------------------------------------------------------

}
